package com.gm.wj.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @program: wj
 * @description: 字符串常用方法工具类
 * @author: Huang_shengjun
 * @create: 2020-12-01 14:24
 **/
public class StringUtil {

    /**
     * EMPTY：返回一个空字符
     * isNullString(String str)：判断是否是空字符串
     * subStringBefore(String str,String separator)：截取特定字符前面的字符串(不区分大小写)
     * subStringAfter(String str,String separator)：截取特定字符后面的字符串(不区分大小写)
     * subStringAfterLastIgnoreCase(String str, String separator)：截取特定字符后面的字符串(不区分大小写)
     * subStringBeforeLastIgnoreCase(String str,String separator)：截取特定字符前面的字符串(不区分大小写)
     * singleQuotoAndEscape (String str)：给字符串前后拼一个单引号，便于sql字符拼接
     * escapeSqlLike(String str)：用于拼接LIKE查询，字符前后加一个%
     * singleQuotoAndEscape(String[] strs)：将数组的字符分割后加上单引号
     * splitToList(String str,String separator)：字符串根据分隔符转换成List
     * splitToListByUpStraight(String str)：按竖线分隔转成List
     * replaceAllToEmpty(String str,String... stringsToReplace)：在str把传入的stringsToReplace替换为空
     * replaceAllTargetValue(String str, String targetValue, String... stringsToReplace)：在str把传入的stringsToReplace替换为targetValue
     * combineToString(Collection<String> coll, String seperator)：将集合转为用 seperator连接的String字符串
     * countMatches(String str,String sub)：sub 在str中出现的次数
     */


    public static final String EMPTY = "";

    /*
     * @Description: 判断是否是空字符串
     * @Param: [str]
     * @return: boolean
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/5/29 13:58
     */
    public static final boolean isNullString(String str){
        return str == null || str.length() == 0 || str.trim().length() == 0;
    }

    /*
     * @Description: 截取特定字符前面的字符串(不区分大小写)，regionMatches方法可控制是否区分
     * 传 'hello' 'l'  返回 'he'
     * @Param: [str, separator]
     * @return: java.lang.String
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/5/29 13:45
     */
    public static String subStringBefore(String str,String separator){
        if (!isNullString(str) && separator != null) {
            if (separator.length() == 0) {
                return "";
            } else {
                int pos = indexOfIgnoreCase(str,separator);
                return pos == -1 ? str : str.substring(0,pos);
            }
        } else {
            return str;
        }
    }

    /*
     * @Description:  截取特定字符后面的字符串(不区分大小写)，regionMatches方法可控制是否区分
     * 传 'hello' 'l'  返回 'lo'
     * @Param: [str, separator]
     * @return: java.lang.String
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 13:53
     */
    public static String subStringAfter(String str,String separator) {
        if (!isNullString(str)) {
            if (isNullString(separator)){
                return "";
            } else {
                int pos = indexOfIgnoreCase(str,separator);
                return pos == -1 ? "" : str.substring(pos + separator.length());
            }
        } else {
            return str;
        }
    }

    /*
     * @Description:  截取特定字符后面的字符串(不区分大小写)
     * @Param: [str, separator]
     * @return: java.lang.String
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 18:54
     */
    public static String subStringAfterLastIgnoreCase(String str, String separator) {

        if (isNullString(str) || isNullString(separator)) {
            return "";
        } else {
            String tempStr = str.toUpperCase();
            String tempSeparator = separator.toUpperCase();
            int index = tempStr.lastIndexOf(tempSeparator);
            if (index == -1) {
                return str;
            } else {
                String result = str.substring(index + separator.length());
                return result;
            }
        }
    }

    /*
     * @Description:  截取特定字符前面的字符串(不区分大小写)
     * @Param: [str, separator]
     * @return: java.lang.String
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 19:30
     */
    public static String subStringBeforeLastIgnoreCase(String str,String separator) {
        if (isNullString(str) || isNullString(separator)) {
            return "";
        } else {
            String tempStr = str.toUpperCase();
            String tempSeparator = separator.toUpperCase();
            int index = tempStr.lastIndexOf(tempSeparator);
            if (index == -1) {
                return str;
            } else {
                String result = str.substring(0,index);
                return result;
            }
        }
    }

    /*
     * @Description:
     * @Param: [str, searchStr]
     * @return: int
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/5/29 14:28
     */
    public static int indexOfIgnoreCase(String str, String searchStr) {
        return indexOfIgnoreCase(str, searchStr, 0);
    }

    /*
     * @Description:
     * @Param: [str, searchStr, startPos]
     * @return: int
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 13:47
     */
    public static int indexOfIgnoreCase(String str, String searchStr, int startPos) {
        int spos = startPos;
        if (str != null && searchStr != null) {
            if (startPos < 0) {
                spos = 0;
            }
            int endLimit = str.length() - searchStr.length() + 1;
            if(spos > endLimit) {
                return -1;
            } else if (searchStr.length() == 0){
                return spos;
            } else {
                for (int i = spos; i < endLimit; i++){
                    if(str.regionMatches(true, i, searchStr, 0,searchStr.length())) {
                        return i;
                    }
                }
                return -1;
            }
        } else {
            return -1;
        }
    }

    /*
     * @Description: 给字符串前后拼一个单引号，便于sql字符拼接
     * @Param: [str]
     * @return: java.lang.String
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 14:02
     */
    public static String singleQuotoAndEscape (String str) {
        if (!isNullString(str)) {
            StringBuffer sb = new StringBuffer(str.length() + 3);
            sb.append("'");
            sb.append(str.replaceAll("'","''"));
            sb.append("'");
            return  sb.toString();
        } else {
            return "''";
        }
    }

    /*
     * @Description: 用于拼接LIKE查询，字符前后加一个%
     * @Param: [likeStr]
     * @return:
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 14:10
     */
    public static String escapeSqlLike(String str) {
        if (!isNullString(str)) {
            StringBuffer sb = new StringBuffer(str.length() + 3);
            sb.append("%");
            sb.append(str.replaceAll("'","''"));
            sb.append("%");
            return  sb.toString();
        } else {
            return "''";
        }
    }

    /*
     * @Description: 将数组的字符分割后加上单引号
     * @Param: [strs]
     * @return: java.lang.String[]
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 14:30
     */
    public static String[] singleQuotoAndEscape(String[] strs) {
        String[] result = new String[strs.length];
        for (int i = 0; i < strs.length; i++) {
            String str = strs[i];
            result[i] = singleQuotoAndEscape(str);
        }
        return result;
    }

    /*
     * @Description: 字符串根据分隔符转换成List
     * @Param: [str, separator]
     * @return: java.util.List<java.lang.String>
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 15:03
     */
    public static List<String> splitToList(String str,String separator) {
        if (str == null) {
            return null;
        } else {
            ArrayList<String> list = new ArrayList();
            String realSeparator = separator;
            if (!str.equals("")) {
                if (!"\\n".startsWith(separator) && !"\\r".startsWith(separator) && !"\\t".startsWith(separator)) {
                    if (!separator.startsWith("+") && !separator.startsWith("*") && !separator.startsWith("?")
                            && !separator.startsWith("\\")) {
                        Pattern p = Pattern.compile(String.valueOf(separator.charAt(0)));
                        Matcher m = p.matcher("~!@#$%^&.<>-|");
                        if (m.find()) {
                            realSeparator = separator.replaceAll(separator,"\\" + separator);
                        }
                    } else {
                        realSeparator = separator.replaceAll(separator,"\\" + separator);
                    }
                } else {
                    realSeparator = separator.replaceAll(separator,"\\" + separator);
                }

                String[] array = str.split(realSeparator);

                for (int i = 0; i < array.length; ++i) {
                    list.add(array[i]);
                }
            }

            return list;
        }
    }

    /*
     * @Description: 按竖线分隔转成List
     * @Param: [str]
     * @return: java.util.List<java.lang.String>
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 17:36
     */
    public static List<String> splitToListByUpStraight(String str) {
        List<String> list = new ArrayList();
        if (str == null) {
            return null;
        } else if (str.equals("")) {
            list.add("");
            return list;
        } else {
            String[] array = str.split("\\|");

            for (int i = 0; i < array.length; ++i) {
                list.add(array[i]);
            }

            return list;
        }
    }

    /*
     * @Description: 在str把传入的stringsToReplace替换为空
     * @Param: [str, stringsToReplace]
     * @return: java.lang.String
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 17:42
     */
    public static String replaceAllToEmpty(String str,String... stringsToReplace) {
        if (str == null) {
            return null;
        } else if (stringsToReplace.length == 0) {
            return str;
        } else {
            String replacedStr = str;
            String[] strsToReplace = stringsToReplace;
            int len = stringsToReplace.length;
            for (int i = 0; i < len; i++) {
                String toReplace = strsToReplace[i];
                if (toReplace != null && !"".equals(toReplace)) {
                    if (!"\\n".startsWith(toReplace) && !"\\r".startsWith(toReplace) && !"\\t".startsWith(toReplace)) {
                        if (!toReplace.startsWith("+") && !toReplace.startsWith("*") && !toReplace.startsWith("?")
                                && !toReplace.startsWith("\\")) {
                            Pattern p = Pattern.compile(String.valueOf(toReplace.charAt(0)));
                            Matcher m = p.matcher("~!@#$%^&.<>-|");
                            if (m.find()) {
                                toReplace = toReplace.replaceAll(toReplace,"\\" + toReplace);
                            }
                        } else {
                            toReplace = toReplace.replaceAll(toReplace,"\\" + toReplace);
                        }
                    } else {
                        toReplace = toReplace.replaceAll(toReplace,"\\" + toReplace);
                    }
                    replacedStr = replacedStr.replaceAll(toReplace, "");
                }
            }
            return replacedStr;
        }
    }

    /*
     * @Description:  在str把传入的stringsToReplace替换为targetValue
     * @Param: [str, targetValue, stringsToReplace]
     * @return: java.lang.String
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 18:30
     */
    public static String replaceAllTargetValue(String str, String targetValue, String... stringsToReplace) {
        if (str == null) {
            return null;
        } else if (stringsToReplace.length == 0) {
            return str;
        } else {
            String replacedStr = str;
            String[] strsToReplace = stringsToReplace;
            int len = stringsToReplace.length;
            for (int i = 0; i < len; i++) {
                String toReplace = strsToReplace[i];
                if (toReplace != null && !"".equals(toReplace)) {
                    if (!"\\n".startsWith(toReplace) && !"\\r".startsWith(toReplace) && !"\\t".startsWith(toReplace)) {
                        if (!toReplace.startsWith("+") && !toReplace.startsWith("*") && !toReplace.startsWith("?")
                                && !toReplace.startsWith("\\")) {
                            Pattern p = Pattern.compile(String.valueOf(toReplace.charAt(0)));
                            Matcher m = p.matcher("~!@#$%^&.<>-|");
                            if (m.find()) {
                                toReplace = toReplace.replaceAll(toReplace,"\\" + toReplace);
                            }
                        } else {
                            toReplace = toReplace.replaceAll(toReplace,"\\" + toReplace);
                        }
                    } else {
                        toReplace = toReplace.replaceAll(toReplace,"\\" + toReplace);
                    }
                    replacedStr = replacedStr.replaceAll(toReplace, targetValue);
                }
            }
            return replacedStr;
        }
    }

    /*
     * @Description: 将集合转为用 seperator连接的String字符串
     * @Param: [coll, seperator]
     * @return: java.lang.String
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 19:39
     */
    public static String combineToString(Collection<String> coll, String seperator) {
        if (coll == null || isNullString(seperator)) {
            return "";
        } else {
            StringBuilder result = new StringBuilder();
            Iterator it = coll.iterator();
            while (it.hasNext()) {
                String str = (String) it.next();
                if (str != null) {
                    result.append(str).append(seperator);
                }
            }

            if (result.length() > seperator.length()) {
                result.setLength(result.length() - seperator.length());
            }

            return result.toString();
        }
    }

    /*
     * @Description:  sub 在str中出现的次数
     * @Param: [str, sub]
     * @return: int
     * @Author: xiongbangwen <Email>xiongbangwen@163.com</Email>
     * @Date: 2020/6/5 19:59
     */
    public static int countMatches(String str,String sub) {
        if (!isNullString(str) && !isNullString(sub)) {
            int count = 0;
            for (int idx = 0; (idx = str.indexOf(sub,idx)) !=-1; idx += sub.length()) {
                ++count;
            }
            return count;
        } else {
            return 0;
        }
    }
}
